package com.todofun.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.todofun.business.tuanke.bo.ApplyRecordService;
import com.todofun.business.tuanke.bo.CampaignService;
import com.todofun.business.tuanke.bo.LessonService;
import com.todofun.business.tuanke.bo.SubscribeUserService;
import com.todofun.business.tuanke.vo.ApplyRecord;
import com.todofun.business.tuanke.vo.Campaign;
import com.todofun.business.tuanke.vo.Lesson;
import com.todofun.common.app.BaseController;
import com.todofun.common.app.BusinessException;
import com.todofun.common.util.CommonUtil;

@RequestMapping(value = "/publishInfo.do")  
@Controller
public class PublishInfoController extends BaseController{
	@Autowired 
	LessonService lessonService;
	
	@Autowired 
	CampaignService campaignService;
	
	@Autowired 
	ApplyRecordService applyRecordService;
	
	@Autowired 
	SubscribeUserService subscribeUserService;
	/**
	 * 注意完善的日志系统，监控系统 
	 */

	private ModelAndView showLessonMsg(String msg){
		String backURL = "publishInfo.do?m=lesson";
		return super.showMsg(msg, backURL);
	}
	
	private ModelAndView showCampaignMsg(String msg){
		String backURL = "publishInfo.do?m=campaign";
		return super.showMsg(msg, backURL);
	}
	
	private ModelAndView getRedirectLessonIndex(){
		return new ModelAndView("redirect:publishInfo.do?m=lesson");
	}
	
	private ModelAndView getRedirectCampaignIndex(){
		return new ModelAndView("redirect:publishInfo.do?m=campaign");
	}
	
	private ModelAndView getRedirectRecordIndex(){
		return new ModelAndView("redirect:publishInfo.do?m=applyrecord");
	}
	
	@RequestMapping(params="m=admin") 
	public ModelAndView admin(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,Long lessonId) throws Exception {  
		return getRedirectCampaignIndex();
    }
	
	
	@RequestMapping(params="m=toggleCampaign") 
	public ModelAndView toggleCampaign(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,Long campaignId) throws Exception {  
		try {
			campaignService.toggleInfo(campaignId);
		} catch (Exception e) {
			return showCampaignMsg(e.getMessage());
		}
		return getRedirectCampaignIndex();
    }
	
	@RequestMapping(params="m=publisCampaign") 
	public ModelAndView publisCampaign(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,Long campaignId) throws Exception {  
		try {
			campaignService.publishCampaign(campaignId);
		} catch (Exception e) {
			return showCampaignMsg(e.getMessage());
		}
		return getRedirectCampaignIndex();
    }
	
	//课程信息
	@RequestMapping(params="m=lesson") 
	public ModelAndView lesson(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {  
		modelMap.put("allLesson", lessonService.listAllLesson());
		return new ModelAndView("tuankemanage/lesson", modelMap);
    }
	
	//添加课程
	@RequestMapping(params="m=addLesson") 
	public ModelAndView addLesson(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {  
		return new ModelAndView("tuankemanage/lesson_add", modelMap);
    }
	
	//修改课程
	@RequestMapping(params="m=editLesson") 
	public ModelAndView editLesson(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,Long lessonId) throws Exception {  
		modelMap.put("data", lessonService.loadLesson(lessonId));
		return new ModelAndView("tuankemanage/lesson_edit", modelMap);
    }
	
	//保存添加的课程
	@RequestMapping(params="m=saveAddLesson") 
	public ModelAndView saveAddLesson(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,Lesson lesson) throws Exception {  
		lessonService.addLesson(lesson);
		return getRedirectLessonIndex();
    }
	
	@RequestMapping(params="m=saveEditLesson") 
	public ModelAndView saveEditLesson(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,Lesson lesson) throws Exception {  
		Lesson lessonInDB = lessonService.loadLesson(lesson.getId());
		if(lessonInDB == null)
			return showLessonMsg("不存在的课程");
		try {
			lessonService.editLesson(lesson);
		} catch (BusinessException e) {
			return showLessonMsg(e.getMessage());
		}
		return getRedirectLessonIndex();
    }
	
	
	@RequestMapping(params="m=delLesson") 
	public ModelAndView delLesson(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,Long lessonId) throws Exception {  
		try {
			lessonService.delLesson(lessonId);
		} catch (Exception e) {
			return showLessonMsg(e.getMessage());
		}
		return getRedirectLessonIndex();
    }
	
	
	//团课活动
	@RequestMapping(params="m=campaign") 
	public ModelAndView campaign(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {  
		modelMap.put("allCampaign", campaignService.listAllCampaign());
		return new ModelAndView("tuankemanage/campaign", modelMap);
    }
	
	//添加活动
	@RequestMapping(params="m=addCampaign") 
	public ModelAndView addCampaign(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {  
		modelMap.put("allLesson", lessonService.listAllLesson());
		return new ModelAndView("tuankemanage/campaign_add", modelMap);
    }
	
	
	//保存添加的活动
	@RequestMapping(params="m=saveAddCampaign") 
	public ModelAndView saveAddCampaign(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,Campaign campaign) throws Exception {  
		campaign.setCreateTime(CommonUtil.getCurrentTime());
		campaignService.save(campaign);
		return getRedirectCampaignIndex();
    }
	
	//修改活动
	@RequestMapping(params="m=editCampaign") 
	public ModelAndView editCampaign(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,Long campaignId) throws Exception {  
		modelMap.put("data", campaignService.getCampaignByID(campaignId));
		modelMap.put("allLesson", lessonService.listAllLesson());
		return new ModelAndView("tuankemanage/campaign_edit", modelMap);
    }
	
	//保存修改活动
	@RequestMapping(params="m=saveEditCampaign") 
	public ModelAndView saveEditCampaign(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,Campaign campaign) throws Exception {  
		campaign.setCreateTime(CommonUtil.getCurrentTime());
		try {
			campaignService.edit(campaign);
		} catch (BusinessException e) {
			return showCampaignMsg(e.getMessage());
		}
		return getRedirectCampaignIndex();
    }
	
	//删除活动
	@RequestMapping(params="m=delCampaign") 
	public ModelAndView delCampaign(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,Long campaignId) throws Exception {  
		try {
			campaignService.delCampaign(campaignId);
		} catch (BusinessException e) {
			return showCampaignMsg(e.getMessage());
		}
		return getRedirectCampaignIndex();
    }
	
	@RequestMapping(params="m=applyrecord") 
	public ModelAndView applyRecord(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {  
		List<ApplyRecord> allRecord = applyRecordService.listAllRecord();
		modelMap.put("allRecord", allRecord);
		return new ModelAndView("tuankemanage/applyrecord", modelMap);
    }
	
	@RequestMapping(params="m=markRecordPayed") 
	public ModelAndView markRecordPayed(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,Long recordId, Boolean isPayed) throws Exception {  
		applyRecordService.markRecordPayed(recordId, isPayed);
		return getRedirectRecordIndex();
    }
	
	@RequestMapping(params="m=markGotMoneyFromRecord") 
	public ModelAndView markGotMoneyFromRecord(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,Long recordId, Boolean isGotMoney) throws Exception {  
		applyRecordService.markGotMoneyFromRecord(recordId, isGotMoney);
		return getRedirectRecordIndex();
    }
	
	@RequestMapping(params="m=subscribeUser") 
	public ModelAndView subscribeUser(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {  
		modelMap.put("allSubscribeUser", subscribeUserService.listAllSubscribeUser());
		return new ModelAndView("tuankemanage/subscribeUser", modelMap);
    }
		
}
